package com.advertise.strutsAction;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.advertise.model.LoginState;
import com.advertise.model.Role;
import com.advertise.model.TreeNode;
import com.advertise.model.User;
import com.advertise.service.MenuService_IFace;
import com.advertise.service.MenuService_Impl;
import com.advertise.service.UserService_IFace;
import com.advertise.tool.ApplicationHelper;
import com.advertise.tool.DhtmlxHelper;
import com.advertise.tool.GlobalUser;
import com.opensymphony.xwork2.ActionContext;

public class LoginPageAction {
	
	private UserService_IFace userService;		// @property 1
	
	private MenuService_IFace menuService;		//	@property 2
	
	
	public MenuService_IFace getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService_IFace menuService) {
		this.menuService = menuService;
	}

	public UserService_IFace getUserService() {
		return userService;
	}

	public void setUserService(UserService_IFace userService) {
		this.userService = userService;
	}

	private User loginUser;

	public User getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(User loginUser) {
		this.loginUser = loginUser;
	}
	
	public String validateLogin(){
		
		//validate the user information
		LoginState loginStatus = userService.validateLogin(loginUser.getLoginName(), loginUser.getPassword());
		
		if(loginStatus.equals(LoginState.LOGINSUCCESS)){
			// set the global user object and can retrive them at any time
			
			GlobalUser globalUser = new GlobalUser();
			globalUser.setUserName(loginUser.getLoginName());
			
			List<TreeNode> userMenus = userService.getUserMenus(loginUser.getLoginName());
			
			globalUser.setUserMenus(userMenus);		
			
			
			
			
			ApplicationHelper.putSession("globalUser", globalUser);
			
			return "success";


		}
		
		else{
			return "error";
		}
		
		
		
		
	}
	
	private String xmlData; 		//this is for return xml page data


	public String getXmlData() {
		return xmlData;
	}

	public void setXmlData(String xmlData) {
		this.xmlData = xmlData;
	}

	//get the user menu when login successfully or click the tree menu node 
	public String getUserMenu(){
		try{
			HttpServletRequest request=ServletActionContext.getRequest();
			int clickedMenuId = 0;
			
			
			if(request.getParameter("id")!=null){
				clickedMenuId = new Integer(request.getParameter("id"));
				
			
			}
		
			GlobalUser globalUser = (GlobalUser)ApplicationHelper.getSessionValue("globalUser");
			
			
			List<TreeNode> userMenus = globalUser.getUserMenus();
			List<TreeNode> currentLevelMenus = menuService.getSubMenusFromMenuList(clickedMenuId,userMenus);
			
			xmlData = DhtmlxHelper.convertListToTreeData(currentLevelMenus, clickedMenuId);
											
			

		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return "success";
	}
	
	public String getMainMenu(){
		
		String currentUser = (String)ApplicationHelper.getSessionValue("globalUser");
		

		return "success";
	}
	
	
	//check login error or not
	private boolean loginError;
	
	public boolean isLoginError() {
		return loginError;
	}

	public void setLoginError(boolean loginError) {
		this.loginError = loginError;
	}

	//Login system method
	public String loginSystem(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			if(request.getParameter("loginError")!=null){
				loginError = Boolean.parseBoolean(request.getParameter("loginError"));
			}
			else{
				loginError = false;
			}
			return "success";
		}
		catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	//Show main page
	
	public String showMainPage(){
		return "success";
	}
	
	
}
